<template>
  <view class="game-container">
    <view class="header">🎯 拖动方块到灰色区域</view>

    <!-- 目标区域 -->
    <view
      class="target-area"
      :style="{ left: target.x + 'px', top: target.y + 'px' }"
    ></view>

    <!-- 可拖动方块 -->
    <view
      class="draggable"
      :style="{
        left: box.x + 'px',
        top: box.y + 'px',
        backgroundColor: boxColor
      }"
      @touchstart="onTouchStart"
      @touchmove="onTouchMove"
      @touchend="onTouchEnd"
    ></view>

    <view class="message">{{ message }}</view>
    <button @click="resetGame" class="restart-btn">重新开始</button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      box: { x: 50, y: 200, size: 80 },
      target: { x: 220, y: 400, size: 100 },
      isDragging: false,
      offset: { x: 0, y: 0 }, // 手指按下时相对方块偏移
      message: "拖动开始吧！",
      boxColor: "#6bc276"
    };
  },
  methods: {
    onTouchStart(e) {
      const touch = e.touches[0];
      this.isDragging = true;

      // 计算手指与方块左上角的偏移
      this.offset.x = touch.clientX - this.box.x;
      this.offset.y = touch.clientY - this.box.y;
    },

    onTouchMove(e) {
      if (!this.isDragging) return;
      const touch = e.touches[0];

      // 按照偏移计算方块新位置
      this.box.x = touch.clientX - this.offset.x;
      this.box.y = touch.clientY - this.offset.y;
    },

    onTouchEnd() {
      this.isDragging = false;

      // 判断是否放入目标
      const boxCenter = {
        x: this.box.x + this.box.size / 2,
        y: this.box.y + this.box.size / 2
      };
      const targetCenter = {
        x: this.target.x + this.target.size / 2,
        y: this.target.y + this.target.size / 2
      };
      const dx = boxCenter.x - targetCenter.x;
      const dy = boxCenter.y - targetCenter.y;
      const distance = Math.sqrt(dx * dx + dy * dy);

      if (distance < this.target.size / 2) {
        this.message = "🎉 成功放置！";
        this.boxColor = "#71bde9";
        uni.showToast({ title: "成功！", icon: "success" });
      } else {
        this.message = "❌ 再试一次！";
        this.boxColor = "#db463e";
      }
    },

    resetGame() {
      this.box = { x: 50, y: 200, size: 80 };
      this.boxColor = "#6bc276";
      this.message = "拖动开始吧！";
    }
  }
};
</script>

<style>
.game-container {
  position: relative;
  width: 100%;
  height: 100vh;
  background: #f0f1f3;
  overflow: hidden;
}
.header {
  text-align: center;
  font-size: 18px;
  margin: 20px 0;
  color: #333;
}
.target-area {
  position: absolute;
  width: 100px;
  height: 100px;
  background: #d3d3d3;
  border: 3px dashed #aaa;
  border-radius: 12px;
}
.draggable {
  position: absolute;
  width: 80px;
  height: 80px;
  border-radius: 10px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}
.message {
  position: absolute;
  bottom: 100px;
  width: 100%;
  text-align: center;
  font-size: 18px;
  color: #333;
}
.restart-btn {
  position: absolute;
  bottom: 40px;
  left: 50%;
  transform: translateX(-50%);
  background: #71bde9;
  color: #fff;
  border-radius: 10px;
  padding: 6px 20px;
}
</style>
